package in.ac.dei.edrp.cms.daoimpl.sendpassword;

import in.ac.dei.edrp.cms.dao.sendpassword.SendPasswordService;
import in.ac.dei.edrp.cms.daoimpl.employee.sendmail;
import in.ac.dei.edrp.cms.domain.sendpassword.SendPasswordInfoGetter;
import in.ac.dei.edrp.cms.utility.TransferInST;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class SendPasswordImpl extends SqlMapClientDaoSupport implements
		SendPasswordService {
	private static Logger logObj = Logger.getLogger(SendPasswordImpl.class);
	private TransactionTemplate transactionTemplate;
	String sep=System.getProperty("file.separator");
	
	ResourceBundle resourceBundle = ResourceBundle.getBundle("in"+sep+"ac"+sep+"dei"+sep+"edrp"+sep+"cms"+sep+"databasesetting"+sep+"MessageProperties",
			new Locale("en", "US"));
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<SendPasswordInfoGetter> getUserList() {
		List<SendPasswordInfoGetter> list = new ArrayList<SendPasswordInfoGetter>();
		try {
			list = getSqlMapClientTemplate().queryForList(
					"sendPassword.getUserList");
		} catch (Exception e) {
			logObj.error(e.getMessage());
		}
		return list;
	}

	public Boolean setApplicantUserInfo(
			final SendPasswordInfoGetter sendPasswordInfoGetter) {
		final String password = TransferInST.generatePassword(4, 8);
		sendPasswordInfoGetter.setPassword(password);
		String s = (String) transactionTemplate.execute(new TransactionCallback() {
			Object savePoint = new Object();

			public String doInTransaction(TransactionStatus ts) {
				savePoint = ts.createSavepoint();
				try {
					if (sendPasswordInfoGetter.getAIorUI().equalsIgnoreCase(
							"AI")) {
						getSqlMapClientTemplate().insert(
								"sendPassword.setApplicantInfo",
								sendPasswordInfoGetter);
					} else {
						getSqlMapClientTemplate().insert(
								"sendPassword.setUserInfo",
								sendPasswordInfoGetter);
					}
					getSqlMapClientTemplate().delete(
							"sendPassword.deleteEmailEntry",
							sendPasswordInfoGetter);
					String msg = "Your Account Information is as follows:\nUser Name: "
							+ sendPasswordInfoGetter.getUserName()
							+ "\nRole:"
							+ sendPasswordInfoGetter.getUserGroupName()
							+ "\nPassword:" + password;
					msg = msg
							+ "\n\n\nClick here to activate your account.\n\n "+resourceBundle.getString("url")+"/sendPassword/activateAccount.htm?userId="
							+ sendPasswordInfoGetter.getUserId() +"&UI="+ sendPasswordInfoGetter.getUniversityId()+"&AIUI="
							+ sendPasswordInfoGetter.getAIorUI()+"&asahs=asnasa&dssssss=%ab$$gfff";
					sendmail.main(msg, sendPasswordInfoGetter.getEmailId(),
							"dei.mhrd.edrp@gmail.com", "Account Details");
					getSqlMapClientTemplate().getDataSource().getConnection()
							.commit();
					return "true";
				} catch (Exception e) {
					ts.rollbackToSavepoint(savePoint);
					logObj.error(e.getMessage());
					return "false";
				}
			}
		});
		return new Boolean(s);
	}

	public Integer updateAIUIStatus(
			SendPasswordInfoGetter sendPasswordInfoGetter) {
		int i=0;
		try {
			if (sendPasswordInfoGetter.getAIorUI().equalsIgnoreCase("AI")) {
				i=getSqlMapClientTemplate().update(
						"sendPassword.updateApplicantStatus",
						sendPasswordInfoGetter);
			} else {
				i=getSqlMapClientTemplate()
						.update("sendPassword.updateUserStatus",
								sendPasswordInfoGetter);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			logObj.error(e.getMessage());
		}
		return i;
	}
}